package com.songqintuan.mall.customer;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.common.util.ValidatorUtil;
import com.songqintuan.mall.constant.MallConstants;
import com.songqintuan.mall.constant.MallSpringBeanConstants;
import com.songqintuan.model.po.Customer;
import com.songqintuan.model.po.CustomerLog;
import com.songqintuan.model.vo.CustomerVo;
import com.songqintuan.model.vo.ProjectVo;
import com.songqintuan.persist.service.hibernate.ICustomerService;

@ManagedBean(name="currentCustomer")
@SessionScoped
public class CurrentCustomerInfoBean implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Customer customer = new Customer();
	private CustomerVo customerVo = new CustomerVo();
	private CustomerLog customerLog = new CustomerLog();
	private List<String> errorMessages = new ArrayList<String>();
	
	private ProjectVo editProject = new ProjectVo();
	public CurrentCustomerInfoBean(){
		
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public CustomerVo getCustomerVo() {
		return customerVo;
	}
	public void setCustomerVo(CustomerVo customerVo) {
		this.customerVo = customerVo;
	}
	public CustomerLog getCustomerLog() {
		return customerLog;
	}
	public void setCustomerLog(CustomerLog customerLog) {
		this.customerLog = customerLog;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}
	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}
	
	public ProjectVo getEditProject() {
		return editProject;
	}
	public void setEditProject(ProjectVo editProject) {
		this.editProject = editProject;
	}
	public String loginOff() {
		//
		this.getErrorMessages().clear();
		this.setCustomer(new Customer());
		this.setCustomerLog(new CustomerLog());
		return MallConstants.INDEX_VIEW;
	}
	
	public void copyCustomerToCustomerVo(){
		BeanCopier copyHere = BeanCopier.create(Customer.class,CustomerVo.class,false);
		copyHere.copy(this.getCustomer(), this.getCustomerVo(), null);
	}
	public String updatePasQueAns(){
		this.getErrorMessages().clear();
		String question = this.getCustomerVo().getPasswordQuestion();
		String answer = this.getCustomerVo().getPasswordAnswer();
		ICustomerService<Customer,String> objService = (ICustomerService<Customer,String>) SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		objService.setEntityClass(Customer.class);
		objService.setPKClass(String.class);
		int result = objService.updateCustomerPasQueAns(question, answer, this.getCustomerVo().getId());
		
		return null;
	}
	public String updateBaseInfo(){
		this.getErrorMessages().clear();
		validateBaseInfo(this.getErrorMessages(), this.getCustomerVo());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		String nickName = this.getCustomerVo().getNickName();
		String name = this.getCustomerVo().getName();
		String sex = this.getCustomerVo().getSex();
		String phone = this.getCustomerVo().getPhone();
		String fax = this.getCustomerVo().getFax();
		String qicq = this.getCustomerVo().getQicq();
		String msn = this.getCustomerVo().getMsn();
		String skype = this.getCustomerVo().getSkype();
		String wangwangNum = this.getCustomerVo().getWangwangNum();
		ICustomerService<Customer,String> objService = (ICustomerService<Customer,String>) SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		objService.setEntityClass(Customer.class);
		objService.setPKClass(String.class);
		String customerId = this.getCustomerVo().getId();
		int result = objService.updateCustomerBaseInfo(nickName, name, sex, phone, fax,
				qicq, msn, skype, wangwangNum, customerId);
		//修改成功后重新load customer
		if (result > 0) {
			Customer theCustomer = objService.findById(customerId);
			this.setCustomer(theCustomer);
			copyCustomerToCustomerVo();
		}
		return null;
	}

	private void validateBaseInfo(List<String> errorMessages, CustomerVo customer){
		String nickNameLabelN18 = MessageFactory.getMessage(
				MallConstants.USER_BUNDLE_NAME,
				"customer.nickname.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		String nameLabelN18 = MessageFactory.getMessage(
				MallConstants.USER_BUNDLE_NAME,
				"customer.name.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		String sexLabelN18 = MessageFactory.getMessage(
				MallConstants.USER_BUNDLE_NAME,
				"customer.sex.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		ValidatorUtil.EmptyValidatorForInput(errorMessages,
				customer.getNickName(), nickNameLabelN18);
		ValidatorUtil.EmptyValidatorForInput(errorMessages,
				customer.getName(), nameLabelN18);
		ValidatorUtil.EmptyValidatorForSelect(errorMessages,
				customer.getSex(), sexLabelN18);
		
	}
}
